#include "drawer.h"
#include "ui_drawer.h"
#include <QDebug>
#include <QColorDialog>
#include <QMenuBar>
#include <QMenu>

drawer::drawer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::drawer)
{
    ui->setupUi(this);
    //这个大小不能改
//    this->setFixedSize(QSize(1200,800));
    //这个大小能改
    this->resize(QSize(1200,800));

    //菜单
    QMenuBar * bar = new QMenuBar(this);
//    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);

    //菜单
    QMenu * begin = bar->addMenu("开始");
    QMenu * edit = bar->addMenu("编辑");
    QMenu * style = bar->addMenu("风格");

    //开始菜单
    QAction * save = begin->addAction("保存");
    QAction * back = begin->addAction("退出");
    connect(back, &QAction::triggered, this, [=](){
        this->close();
    });

    //编辑菜单
    QAction * deleteOneLine = edit->addAction("删除上一条");
    connect(deleteOneLine, &QAction::triggered, this, [=](){
        _lines.pop_back();
        pens.pop_back();
        this->update();
    });

    QAction * deleteAllLine = edit->addAction("删除全部");
    connect(deleteAllLine, &QAction::triggered, this, [=](){
        while(!_lines.empty() || !pens.empty())
        {
            _lines.pop_back();
            pens.pop_back();
        }
        this->update();
    });


    //风格菜单
    QAction * colorAction = style->addAction("颜色");
    connect(colorAction, &QAction::triggered, this, [=](){
        QColor color = QColorDialog::getColor();
        lastpen.setColor(color);
    });

//    QAction * widthAction = style->addAction("粗细");

    QMenu * widthAction = style->addMenu("粗细选择");

    QAction * small = widthAction->addAction("1xp");
    connect(small, &QAction::triggered, this, [=](){
        lastpen.setWidth(1);
    });

    QAction * small1 = widthAction->addAction("5xp");
    connect(small1, &QAction::triggered, this, [=](){
        lastpen.setWidth(5);
    });

    QAction * mind = widthAction->addAction("10xp");
    connect(mind, &QAction::triggered, this, [=](){
        lastpen.setWidth(10);
    });

    QAction * wide = widthAction->addAction("20xp");
    connect(wide, &QAction::triggered, this, [=](){
        lastpen.setWidth(20);
    });

    QAction * custom = widthAction->addAction("自定义");
    connect(wide, &QAction::triggered, this, [=](){
        //
    });

    QMenu * penstyle = style->addMenu("画笔风格");

    QAction * solidline = penstyle->addAction("实线");
    connect(solidline, &QAction::triggered, this, [=](){
        lastpen.setStyle(Qt::SolidLine);
    });
    QAction * dashline = penstyle->addAction("虚线");
    connect(dashline, &QAction::triggered, this, [=](){
        lastpen.setStyle(Qt::DashLine);
    });
    QAction * dotline = penstyle->addAction("点线");
    connect(dotline, &QAction::triggered, this, [=](){
        lastpen.setStyle(Qt::DotLine);
    });
    QAction * dashdotline = penstyle->addAction("点实线");
    connect(dashdotline, &QAction::triggered, this, [=](){
        lastpen.setStyle(Qt::DashDotLine);
    });
    QAction * dashdotdotline = penstyle->addAction("点点实线");
    connect(dashdotdotline, &QAction::triggered, this, [=](){
        lastpen.setStyle(Qt::DashDotDotLine);
    });
    QAction * customdashLine = penstyle->addAction("经典点实线");
    connect(customdashLine, &QAction::triggered, this, [=](){
        lastpen.setStyle(Qt::CustomDashLine);
    });

    QMenu * capstyle = style->addMenu("笔头风格");
    QAction * squarecap = capstyle->addAction("方头");
    connect(squarecap, &QAction::triggered, this, [=](){
        lastpen.setCapStyle(Qt::SquareCap);
    });
    QAction * flatcap = capstyle->addAction("扁头");
    connect(flatcap, &QAction::triggered, this, [=](){
        lastpen.setCapStyle(Qt::FlatCap);
    });
    QAction * roundcap = capstyle->addAction("圆头");
    connect(roundcap, &QAction::triggered, this, [=](){
        lastpen.setCapStyle(Qt::RoundCap);
    });

    QMenu * penjoinstyle = style->addMenu("拐角风格");
    QAction * beveljoin = penjoinstyle->addAction("平角");
    connect(beveljoin, &QAction::triggered, this, [=](){
        lastpen.setJoinStyle(Qt::BevelJoin);
    });
    QAction * miterjoin = penjoinstyle->addAction("尖角");
    connect(miterjoin, &QAction::triggered, this, [=](){
        lastpen.setJoinStyle(Qt::MiterJoin);
    });
    QAction * roundjoin = penjoinstyle->addAction("圆角");
    connect(roundjoin, &QAction::triggered, this, [=](){
        lastpen.setJoinStyle(Qt::RoundJoin);
    });


//    color = QColorDialog::getColor();
//    qDebug() << color;
}

drawer::~drawer()
{
    delete ui;
}

void drawer::paintEvent(QPaintEvent* ev)
{
    QPainter painter(this);
//   shujujiegou  QVector<QColor> colors;




    for(int i = 0 ; i < _lines.size(); i++)
    {
        const QVector<QPoint> &line = _lines.at(i);
//        QPen pen(colors.at(i));
//        pen.setBrush();
//        pen.setWidth(10);
        painter.setPen(pens.at(i));

        for (int j = 0 ; j < line.size()-1; j++)
        {
            painter.drawLine(line.at(j),line.at(j+1));
        }
    }
    qDebug() << "绘图事件调用";
//    p.drawEllipse(QPoint(250,250),200,200);
//    p.setBrush(QBrush(Qt::green));
//    p.translate(200,200);
//    p.drawLine(QPoint(0,50),QPoint(29.39,-40.45));
//    p.drawLine(QPoint(29.39,-40.45),QPoint(-47.55,16.08));
//    p.drawLine(QPoint(-47.55,16.08),QPoint(47.55,15.45));
//    p.drawLine(QPoint(47.55,15.45),QPoint(-23.39,-40.16));
//    p.drawLine(QPoint(-23.39,-40.16),QPoint(0,50));


}



void drawer::mouseMoveEvent(QMouseEvent* ev)
{
    qDebug() << "移动事件";
    if(_lines.size() == 0)
    {
        QVector<QPoint> line;
        _lines.append(line);
    }

    QVector<QPoint> &lastline = _lines.last();
    lastline.append(ev->pos());
    //对这个类的事件进行跟新
    this->update();
}

void drawer::mousePressEvent(QMouseEvent* ev)
{
    qDebug() << "按下事件";
    QVector<QPoint> line;
    _lines.append(line);    //就是push_back()

    //设置这条线的颜色
    pens.append(lastpen);

    //取出线的引用
    QVector<QPoint> &lastLine = _lines.last();
    //调用pos事件，返回QPoint
    lastLine.append(ev->pos());
}

void drawer::mouseReleaseEvent(QMouseEvent* ev)
{
    qDebug() << "释放事件";
    QVector<QPoint> &lastLine = _lines.last();
    lastLine.append(ev->pos());
//    this->update();
}
